﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace MediaWiki.Office.Model
{
  public abstract class RunContent : Content
  {
    public new Run Parent
    {
      get { return (Run)base.Parent; }
      set { base.Parent = value; }
    }

    public RunContent(Run parent, XElement element)
      : base(parent)
    {
      if (element == null)
        throw new ArgumentNullException("element");
    }

    public static IList<RunContent> Create(Run parent, IEnumerable<XElement> contentElements)
    {
      List<RunContent> content = contentElements
                                     //.Where(e => e.Name == Run.Name || e.Name == Hyperlink.Name)
                                     .Where(e => e.Name.LocalName != "rPr")
                                     .Select(e => RunContent.Create(parent, e))
                                     .Where(o => o != null)
                                     .ToList();

      return content;
    }

    public static RunContent Create(Run parent, XElement runContentElement)
    {
      if (parent == null)
        throw new ArgumentNullException("parent");
      if (runContentElement == null)
        throw new ArgumentNullException("runContentElement");

      switch (runContentElement.Name.LocalName)
      {
        case LineBreak.LocalName:
          return new LineBreak(parent, runContentElement);// new Paragraph(parent, runContentElement);
        case Text.LocalName:
          return new Text(parent, runContentElement);
        case LastRenderedPageBreak.LocalName:
          return new LastRenderedPageBreak(parent, runContentElement);
        case CarriageReturn.LocalName:
          return new CarriageReturn(parent, runContentElement);
        case Image.DrawingMLLocalName:
          return new Image(parent, runContentElement);
        case Image.VmlLocalName:
          return null;
        case Tab.LocalName:
          return new Tab(parent, runContentElement);
        case "fldChar":
        case "instrText":
          // complex fields. ignore
        case "ptab":
          // absolute tab. ignore
          return null;
        default:
          throw new ArgumentOutOfRangeException(runContentElement.Name.LocalName);
      }
    }

    public abstract RunContentType ContentType { get; }
  }
}